var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        headerlist: [],
        list: [],
        searchlist: [],
        form: {
            type: 1, //2-不选择房间
            keywords: '',
            parentid: 1,
            selecttype: 2, //1-单选 2-多选
            isadd: 0,
            projectlevel: 0,
            fromtype: 0 //1-水电抄表 2-公表 3-总表 4-楼宇管家
        },
        selectlist: [],
        canopen: true
    },
    methods: {
        get_data: function() {
            var that = this;
            that.selectlist = [];
            var options = {};
            options.action = "getprojectlistbyparentid";
            options.parentid = that.form.parentid;
            options.type = that.form.type;
            options.keywords = that.form.keywords;
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function(ret) {});
            ns.post(options, function(succeed, data, err) {
                toast.hide();
                console.log(JSON.stringify(data));
                if (succeed) {
                    that.list = [];
                    that.searchlist = [];
                    if (that.form.keywords != '') {
                        that.searchlist = data.list;
                        return;
                    }
                    that.list = data.list;
                    that.headerlist = data.headerlist;
                    for (var i = 0; i < that.list.length; i++) {
                        for (var j = 0; j < that.selectlist.length; j++) {
                            if (that.selectlist[j].id == that.list[i].id)
                                that.list[i].isselect = true;
                        }
                    }
                } else if (err) {
                    that.list = [];
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_search: function() {
            this.get_data();
        },
        clear_searchVal: function () {
            var that = this;
            document.getElementById("search-input").value = '';
        },
        do_open: function(item) {
            var that = this;
            if (item.id == that.form.parentid) {
                return;
            }
            if (!item.isParent || that.form.projectlevel == 1) {
                that.do_choose(item);
                return;
            }
            that.form.parentid = item.id;
            that.get_data();
        },
        do_choose: function(item) {
            var that = this;
            item.isselect = !item.isselect;
            if (that.form.selecttype == 1 || item.levelid <= 2) {
                for (var i = 0; i < that.list.length; i++) {
                    if (that.list[i].id != item.id) {
                        that.list[i].isselect = false;
                        continue;
                    }
                }
                that.selectlist = [];
                if (item.isselect) {
                    that.selectlist.push(item);
                }
            } else {
                var canadd = true;
                var canremove = false;
                var removeindex = 0;
                for (var i = 0; i < that.selectlist.length; i++) {
                    if (that.selectlist[i].id == item.id) {
                        canadd = false;
                        canremove = true;
                        removeindex = i;
                    }
                }
                if (item.isselect) {
                    if (canadd) {
                        that.selectlist.push(item);
                    }
                } else {
                    if (canremove) {
                        that.selectlist.splice(removeindex, 1);
                    }
                }
            }
        },
        do_choose_search: function(item) {
            var that = this;
            item.isselect = !item.isselect;
            if (that.form.selecttype == 1) {
                for (var i = 0; i < that.searchlist.length; i++) {
                    if (that.searchlist[i].id != item.id) {
                        that.searchlist[i].isselect = false;
                        continue;
                    }
                }
                that.selectlist = [];
                if (item.isselect) {
                    that.selectlist.push(item);
                }
            } else {
                var canadd = true;
                var canremove = false;
                var removeindex = 0;
                for (var i = 0; i < that.selectlist.length; i++) {
                    if (that.selectlist[i].id == item.id) {
                        canadd = false;
                        canremove = true;
                        removeindex = i;
                    }
                }
                if (item.isselect) {
                    if (canadd) {
                        that.selectlist.push(item);
                    }
                } else {
                    if (canremove) {
                        that.selectlist.splice(removeindex, 1);
                    }
                }
            }
        },
        do_complete: function() {
            var that = this;
            that.canopen = true;
            that.do_save();
        },
        do_send_event: function() {
            var that = this;
            that.canopen = false;
            that.do_save();
        },
        do_save: function() {
            var that = this;
            if (that.selectlist.length == 0) {
                api.toast({
                    msg: '请选择资源',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var roomid = 0;
            var projectid = 0;
            var name = '';
            var namenum='';
            var roomidlist = [];
            var projectidlist = [];
            var namelist = [];
            if (that.form.selecttype == 1) {
                if (that.selectlist[0].isParent) {
                    projectid = that.selectlist[0].id;
                    projectidlist.push(projectid);
                } else {
                    roomid = that.selectlist[0].id;
                    roomidlist.push(roomid);
                }
                name = that.selectlist[0].fullname;
                namenum=that.selectlist[0].name;
            } else {
                for (var i = 0; i < that.selectlist.length; i++) {
                    if (that.selectlist[i].isParent) {
                        projectidlist.push(that.selectlist[i].id);
                    } else {
                        roomidlist.push(that.selectlist[i].id);
                    }
                    namelist.push(that.selectlist[i].fullname);
                }
                name = namelist.join(',');
            }
            if (that.form.isadd == 1) {
                var options = {};
                options.action = "dosavemyproject";
                options.roomid = roomid;
                options.projectid = projectid;
                options.roomids = JSON.stringify(roomidlist);
                options.projectids = JSON.stringify(projectidlist);
                ns.post(options, function(succeed, data, err) {
                    if (succeed) {
                        api.sendEvent({
                            name: 'do_add_project_complete'
                        });

                        //api.closeWin();

                        var title = '楼宇管家';
                        switch (that.form.fromtype) {
                            case 1:
                                title = '水电抄表';
                                break;
                            case 2:
                                title = '公表抄表';
                                break;
                            case 3:
                                title = '总表抄表';
                                break;
                            default:
                                title = '楼宇管家';
                                break;
                        }
                        ns.openWin('chooseproject_frm', title, {
                            fromtype: that.form.fromtype,
                        });


                    } else if (err) {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }, {
                    toast: true,
                    toastmsg: '提交中'
                });
                return;
            }
            var selectheaderlist = that.get_select_header_list();
            var params = {
                roomid: roomid,
                projectid: projectid,
                roomids: JSON.stringify(roomidlist),
                projectids: JSON.stringify(projectidlist),
                name: name,
                namenum:namenum,
                headerlist: JSON.stringify(selectheaderlist)
            }
            if (that.form.fromtype > 0 && that.canopen) {
                var openurl = '';
                var title = '';
                if (that.form.fromtype == 1) {
                    openurl = 'meterpoingwrite_frm';
                    title = '水电抄表';
                } else if (that.form.fromtype == 2) {
                    openurl = 'meterpublicwrite_frm';
                    title = '公表抄表';
                } else if (that.form.fromtype == 3) {
                    openurl = 'metersumwrite_frm';
                    title = '总表抄表';
                } else if (that.form.fromtype == 4) {
                    openurl = 'shoufeiguanli_frm';
                    title = '楼宇管家';
                }
                ns.openWin(openurl, title, params)
                return;
            }
            setTimeout(function() {
                var eventName = 'do_choose_project_complete';
                api.sendEvent({
                    name: eventName,
                    extra: params
                });
                api.closeWin();
            }, 500)
        },
        get_select_header_list: function() {
            var that = this;
            var list = [];
            for (var i = 1; i < that.headerlist.length; i++) {
                var item = that.headerlist[i];
                if (i == that.headerlist.length - 1) {
                    var name = item.name.replace('...', '');
                    list.push({
                        id: item.id,
                        name: name,
                        isParent: item.isParent
                    });
                    continue;
                }
                list.push(that.headerlist[i]);
            }
            if (that.selectlist.length == 1) {
                var item = that.selectlist[0];
                var name = item.name.replace('...', '');
                list.push({
                    id: item.id,
                    name: name,
                    isParent: item.isParent
                });
            }
            return list;
        },
        getlabelwidth: function(item) {
            var that = this;
            if (!item.isParent) {
                return {};
            }
            return {
                width: (item.name.length * 15) + 'px',
                maxWidth: '60px'
            };
        }
    }
});
ns = window.Foresight.Util;
apiready = function() {
    api.parseTapmode();
    toast = new auiToast();
    app.form.type = api.pageParam.type || 1;
    app.form.selecttype = api.pageParam.selecttype || 1;
    app.form.isadd = api.pageParam.isadd || 0;
    app.form.parentid = api.pageParam.parentid || 1;
    app.form.projectlevel = api.pageParam.projectlevel || 0;
    app.form.fromtype = api.pageParam.fromtype || 0;
    ns.init_search();
    app.get_data();
    api.addEventListener({
        name: 'do_send_project_parentid'
    }, function(ret, err) {
        app.form.parentid = ret.value.parentid;
        app.get_data();
    });

}
ns.init_search();
